#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;

class Grand
{
private:
	int hold;
public:
	Grand(int h = 0) : hold(h) {}
	virtual void Speek() const { cout << "I am a grand class class !\n"; }
	virtual int Value() const { return hold; }
};

class Superd :public Grand
{
public:
	Superd(int h = 0) : Grand(h) {}
	void Speak() const { cout << "I am a superd class !!!\n"; }
	virtual void Say() const { cout << "I hold the superd value of " << Value() << "!\n"; }
};

class Magnifficent : public Superd
{
private:
	char ch;
public:
	Magnifficent(int h = 0, char c = 'A') : Superd(h), ch(c) {}
	void Speak() const { cout << "I am a magnificent class !!!\n"; }
	void Say() const { cout << "I hold the character " << ch << " and the integer" << Value() << "!\n"; }
};

Grand *GetOne()
{
	Grand * p;

	switch (std::rand() % 3)
	{
	case 0:
		p = new Grand(std::rand() % 100);
		break;
	case 1:
		p = new Superd(std::rand() % 100);
		break;
	case 2:
		p = new Magnifficent(std::rand() % 100, 'A'+ std::rand() % 26);
		break;
	}
	return p;
}

int main()
{

	std::srand(std::time(0));

	Grand *pg;
	Superd *ps;

	for (int i = 0; i < 5; i++)
	{
		pg = GetOne();
		pg->Speek();
		if (ps = dynamic_cast <Superd *> (pg))
		{
			ps->Say();
		}
	}

	std::cin.get();
    return 0;
}

